package com.ruoyi.common.core.config;

import java.util.concurrent.Executor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * <p>
 * 作用：异步线程管理器
 * </p>
 *
 * @author zhanglianbao
 * @since 2025-02-13 22:04
 */
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

  @Bean(name = "asyncExecutor")
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(8); // 核心线程数
    executor.setMaxPoolSize(20); // 最大线程数
    executor.setQueueCapacity(100); // 队列容量
    // 线程名称前缀
    executor.setThreadNamePrefix("Customer-Async-Thread-");
    // 拒绝策略
    executor.setRejectedExecutionHandler(
        new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
  }
}
